package com.examsystem.dao;


import com.examsystem.pojo.PictureQuestion;
import com.examsystem.pojo.Question;
import com.examsystem.pojo.vo.QuestionVo;
import org.apache.ibatis.annotations.*;

import java.util.List;
@Mapper
public interface QuestionMapper {

    /*
    学生端
     */

    /**添加一个Question
     * @param question 传入question类
     * @return int
     */
    int addQuestion(Question question);
    /**根据id删除一个Question
     * @param id 传入id
     * @return int
     */
    int deleteQuestionById(int id);

    /**更新Question
     * @param question 传入question类
     * @return int
     */
    int updateQuestion(Question question);
    /**根据id查询,返回一个Question
     * @param id 传入id
     * @return int
     */
    Question queryQuestionById(int id);
    /**查询全部Question,返回list集合
     * @return List<Question>
     */
    List<Question> queryAllQuestion();

    List<Question> queryQuestionsByPaperId(int paperId);

    List<Question> queryQuestionsByPaperIdAndUser(@Param("paperId") int paperId,@Param("userId") int userId);



    /*
    教师端
     */


    /**
     * 添加试题【到试题库中】
     * @param question 试题对象
     * @return  1：添加成功 0：失败
     */
    @Insert("insert into question values (0,#{questionType},#{questionAnswer1}," +
            "#{questionScore}," +
            "null,#{questionContent},#{questionParse1})")
    int insertQuestion(Question question);

    /**
     * 给试题添加图片解析/答案
     * @param pictureQuestion 试题图片
     * @return  【1：成功  0：失败】
     */
    @Options(useGeneratedKeys = true,keyProperty = "pictureId",keyColumn = "picture_id")
    @Insert("insert into picture_question values (0,#{pictureRouteA},#{pictureQuestion},#{pictureRouteP})")
    int insertQuestionPic(PictureQuestion pictureQuestion);

    /**
     * 给试题插入图片插入
     * @param questionId  试题id
     * @param questionPic  图片中【解析/答案】id
     * @return 【1：成功  0：失败】
     */
    @Update("update question set question_picture = #{questionPic} where question_id = #{questionId}")
    int updateQuestionPic(@Param("questionId") int questionId,@Param("questionPic") int questionPic);

    /**
     * 获取刚插入picture_question的id
     * @return  id
     */
    @Select("select max(picture_id) from picture_question")
    int selectPicId();

    /**
     * 显示所有的试题
     * @return  question对象
     */
    @Select("select *from questionV")
    @Results({
            @Result(column = "questionId" ,property = "questionId"),
            @Result(column = "questionContent" ,property = "questionContent"),
            @Result(column = "questionType" ,property = "questionType"),
            @Result(column = "questionA1" ,property = "questionAnswer1"),
            @Result(column = "questionA2",property = "questionAnswer2"),
            @Result(column = "questionP1",property = "questionParse1"),
            @Result(column = "questionP2",property = "questionParse2"),
            @Result(column = "question_score" ,property = "questionScore")
    })
    List<Question> showAllQuestions();




    /**
     * 查找question对象
     * @param paperId 试卷id
     * @return  question对象
     */
    @Select("select *from questionV where paperId = #{paperId}")
    @Results({
            @Result(column = "questionId" ,property = "question.questionId"),
            @Result(column = "questionContent" ,property = "question.questionContent"),
            @Result(column = "questionType" ,property = "question.questionType"),
            @Result(column = "questionA1" ,property = "question.questionAnswer1"),
            @Result(column = "questionA2",property = "questionA2"),
            @Result(column = "questionP1",property = "question.questionParse1"),
            @Result(column = "questionP2",property = "questionP2"),
            @Result(column = "questionScore" ,property = "question.questionScore"),
    })
    List<QuestionVo> findQuestion(int paperId);





    /**
     * 往试卷中添加试题
     * @param paperId  试卷id
     * @param questionId  试题id
     * @return  1：添加成功   0：添加失败
     */
    @Insert("insert into exam.paper_question values (null,#{questionId},#{paperId},0)")
    int addQtoP(@Param("paperId") int paperId,@Param("questionId") int questionId);

    /**
     * 取消往试卷中添加的试题
     * @param paperId  试卷id
     * @param questionId  试题id
     * @return 1：删除成功  0：删除失败
     */
    @Delete("delete from exam.paper_question where pq_question = #{questionId} and pq_paper = #{paperId}")
    int deleteQtoP(@Param("paperId") int paperId,@Param("questionId") int questionId);


    /**
     * 数据库总共有多少道题
     * @return int number
     */
    @Select("select count(*) from exam.question ")
    long sumQuestions();

    /**
     * 随机获取questionId
     * @param number  需要的个数
     * @return  list集合的questionId
     */
    @Select("SELECT * FROM user ORDER BY rand() LIMIT #{number}  FOR UPDATE")
    List<Integer> questionIds(int number);

}
